<?php
namespace Admin\Controller;

class ConfigController extends CommonController{
	public  $exec_comfunc  = 'index,add,edit,delete';//index,add,edit,delete

	public function index(){
		parent::index('','','type,order_id');
	}
	
	//设置参数
	public function setting(){
		$type = $this->_get('type','','system');
		$list = $this->get_setting($type);

		if(IS_POST){
			foreach ($list as $rs) {
				unset($data,$where);
				$value = ($_POST[$rs['name']]);
				if(is_array($value)){
					$value = implode(',', $value);
					if(substr($value, 0, 2) == '0,'){
						$value = substr($value, 2);
					}
				}
				$value = trim($value);

				$where['type'] = $type;
				$where['name'] = $rs['name'];
				if(D('Config')->where($where)->count()){
					$data['val'] = $value;
					D('Config')->where($where)->save($data);
				}else{
					$rs['type'] = $type;
					$rs['val']  = $value;
					D('Config')->add($rs);
				}
			}
			$this->success('设置成功！');
		}


		$this->assign('list',$list);
		$this->display();
	}
	private function get_setting($type = 'sms'){
		$list = C('setting');

		$conf_data = $list[$type];
		$where['type'] = $type;
		foreach ($conf_data as $rs) {
			$where['name'] = $rs['name'];
			$val = D('Config')->where($where)->getField('val');
			$rs['val'] = $val != NULL ? $val : $rs['val'];
			$new_list[] = $rs;
		}
		// var_dump($new_list);
		return $new_list;
	}
}
?>